#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 2e5 + 10, INF = 1e9;

int n, w, m;
char str[N];
int presum[N];
int f[10], nf[10];

void init(){
    for(int i = 0; i < 10; i ++){
        f[i] = nf[i] = INF;
    }
}

void solve(){
    init();
    scanf("%s", str + 1);
    n = strlen(str + 1);

    for(int i = 1; i <= n; i ++){
        int x = str[i] - '0';
        presum[i] = presum[i - 1] + x;
    }

    scanf("%d%d", &w, &m);

    for(int i = 1; i + w - 1 <= n; i ++){
        int x = (presum[i + w - 1] - presum[i - 1]) % 9;

        if(f[x] == INF){
            f[x] = i;
        }else if(nf[x] == INF){
            nf[x] = i;
        }   
    }

    int l, r, k;


    while(m --){
        int res1 = INF, res2 = INF;
        scanf("%d%d%d", &l, &r, &k);
        k = k % 9;
        int x = (presum[r] - presum[l - 1]) % 9;
        for(int i = 0; i < 10; i ++){
            if(f[i] == INF) continue;
            
            int a = i * x % 9;
            int b = ((k - a) >= 0) ? k - a : ((k - a) % 9 + 9) % 9;


            if(i != b){
                if(f[b] != INF){
                    if(res1 > f[i]){
                        res1 = f[i];
                        res2 = f[b];
                    }else if(res1 == f[i]){
                        res2 = min(f[b], res2);
                    }
                }
            }else if(i == b){
                if(nf[b] != INF){
                    if(res1 > f[i]){
                        res1 = f[i];
                        res2 = nf[b];
                    }else if(res1 == f[i]){
                        res2 = min(f[b], res2);
                    }
                }
            }
        }

        if(res1 == INF || res2 == INF){
            printf("-1 -1\n");
        }else{
            printf("%d %d\n", res1, res2);
        }
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    int T;
    scanf("%d", &T);
    while(T--){
        solve();
    }

    return 0;
}